package com.test.reggieadmin.model.fragment.flgl;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;

import com.lxj.xpopup.XPopup;
import com.test.reggieadmin.R;
import com.test.reggieadmin.base.BaseFragment;
import com.test.reggieadmin.base.BaseRVAdapter;
import com.test.reggieadmin.base.BaseRVHolder;
import com.test.reggieadmin.bean.BaseBean;
import com.test.reggieadmin.bean.GetDataType;
import com.test.reggieadmin.bean.PageCategoryBean;
import com.test.reggieadmin.databinding.FragmentFlglBinding;
import com.test.reggieadmin.model.category.AddCategoryActivity;
import com.yechaoa.yutils.ToastUtil;
import com.yechaoa.yutils.YUtils;

import java.util.List;

/**
 * 分类管理
 */
public class FlglFragment extends BaseFragment<FragmentFlglBinding, FlglPrensenter> implements FlglView {

    private BaseRVAdapter<PageCategoryBean.DataBean> adapter;
    private long skip = 0;
    private int pageSize = 10;
    private int pos;
    private int ADD = 201;
    private int UPDATE = 202;

    @Override
    protected FlglPrensenter createPresenter() {
        return new FlglPrensenter(this);
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        viewBinding.fabAdd.setOnClickListener(v -> {//添加分类
            Intent intent = new Intent(getActivity(), AddCategoryActivity.class);
            startActivityForResult(intent, ADD);
        });

        YUtils.showLoading("加载中...");
        presenter.pageCategory(skip, pageSize, GetDataType.GETDATA);//分页查询分类
        adapter = new BaseRVAdapter<PageCategoryBean.DataBean>(R.layout.item_category) {
            @Override
            public void onBindVH(BaseRVHolder holder, PageCategoryBean.DataBean data, int position) {
                TextView tv_type = holder.getView(R.id.tv_type);
                TextView tv_delete = holder.getView(R.id.tv_delete);
                LinearLayout ll_cotent = holder.getView(R.id.ll_cotent);

                holder.setText(R.id.tv_name, data.getName());
                if (data.getType() == 1) {
                    tv_type.setText("菜品分类");
                } else if (data.getType() == 2) {
                    tv_type.setText("套餐分类");
                }
                holder.setText(R.id.tv_sort, "排序：" + data.getSort());
                holder.setText(R.id.tv_updateTime, data.getUpdateTime());

                tv_delete.setOnClickListener(v -> {//删除分类
                    new XPopup.Builder(getActivity()).asConfirm("提示", "确定删除该分类吗",
                            () -> {
                                pos = position;
                                YUtils.showLoading("提交中...");
                                presenter.deleteCategory(data.getId());
                            })
                            .show();
                });

                ll_cotent.setOnClickListener(v -> {//修改分类
                    pos = position;
                    Intent intent = new Intent(getActivity(), AddCategoryActivity.class);
                    intent.putExtra("id", data.getId());
                    startActivityForResult(intent, UPDATE);
                });
            }
        };

        empty("暂无数据");

        viewBinding.mRefreshLayout.setOnRefreshListener(refreshLayout -> {//下拉刷新
            skip = 0;
            presenter.pageCategory(skip, pageSize, GetDataType.REFRESH);
        });

        viewBinding.mRefreshLayout.setOnLoadMoreListener(refreshLayout -> {//上拉加载更多
            skip = adapter.getData().size();
            presenter.pageCategory(skip, pageSize, GetDataType.LOADMORE);
        });

        viewBinding.mRecyclerView.setAdapter(adapter);
        viewBinding.mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 1));//列数设置
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ADD && resultCode == Activity.RESULT_OK) {
            skip = 0;
            presenter.pageCategory(skip, pageSize, GetDataType.REFRESH);
        } else if (requestCode == UPDATE && resultCode == Activity.RESULT_OK) {
            skip = 0;
            presenter.pageCategory(skip, pageSize, GetDataType.REFRESH);
        }
    }

    @Override
    public void pageCategorySuccess(PageCategoryBean bean, int type) {
        YUtils.hideLoading();

        if (bean.getCode() == 200) {
            List<PageCategoryBean.DataBean> pageList = bean.getData();
            switch (type) {
                case GetDataType.GETDATA://获取数据成功
                    adapter.setNewData(pageList);
                    viewBinding.mRefreshLayout.setNoMoreData(false);
                    break;
                case GetDataType.REFRESH://刷新成功
                    adapter.setNewData(pageList);
                    viewBinding.mRefreshLayout.finishRefresh();
                    viewBinding.mRefreshLayout.setNoMoreData(false);
                    break;
                case GetDataType.LOADMORE://加载成功
                    if (pageList != null && !pageList.isEmpty()) {
                        adapter.addData(pageList);
                        if (pageList.size() < pageSize) {
                            viewBinding.mRefreshLayout.finishLoadMoreWithNoMoreData();
                        } else {
                            viewBinding.mRefreshLayout.finishLoadMore();
                        }
                    } else {
                        viewBinding.mRefreshLayout.finishLoadMoreWithNoMoreData();
                    }
                    break;
            }
        } else {
            ToastUtil.showCenter(bean.getMsg());
            viewBinding.mRefreshLayout.finishRefresh();
            viewBinding.mRefreshLayout.finishLoadMore();
        }
    }

    @Override
    public void pageCategoryError(String msg) {
        YUtils.hideLoading();
        viewBinding.mRefreshLayout.finishRefresh();
        viewBinding.mRefreshLayout.finishLoadMore();

        empty(msg);
    }

    @Override
    public void deleteCategorySuccess(BaseBean bean) {
        YUtils.hideLoading();
        ToastUtil.showCenter(bean.getMsg());
        if (bean.getCode() == 200) {
            adapter.remove(pos);
        }
    }

    @Override
    public void onError(String msg) {
        YUtils.hideLoading();
        ToastUtil.showCenter(msg);
    }

    private void empty(String msg) {
        View empty = LayoutInflater.from(getActivity()).inflate(R.layout.layout_no_data, null, false);
        TextView tv_tip = empty.findViewById(R.id.tv_tip);
        tv_tip.setText(msg);
        adapter.setEmptyView(empty);
    }
}